import { Module } from 'vuex'
import { redactProductType, GlobalDataProps } from '@/store/storeType'
import { getFn } from '@/store/store_actions_axiosFn'

const redactProductModule: Module<redactProductType, GlobalDataProps> = {
  namespaced: true,
  state: () => ({
    redactData: {}
  }),
  mutations: {
    getProductData (state, data) {
      if (data.status === 200) {
        state.redactData = data.data.data
      }
    },
    getAttributes (state, data) {
      if (data.status === 200) {
        if (data.data.data[0].attr_sel === 'many') {
          data.data.data.map((item: any) => {
            item.attr_vals = item.attr_vals.length === 0 ? [] : item.attr_vals.split(',')
            return item
          })
          state.redactData.attr_vals = data.data.data
        } else {
          state.redactData.attrs = data.data.data
        }
      }
    },
  },
  actions: {
    getProductData ({ commit }, id) {
      return getFn(`goods/${id}`, 'getProductData', commit)
    },
    getAttributes ({ commit }, parmas) {
      return getFn(`categories/${parmas.cid}/attributes`, 'getAttributes', commit, { sel: parmas.sel })
    },
  }
}

export default redactProductModule